﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace WinformPratice
{
    public partial class GetCategory : Form
    {
        public GetCategory()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String url = textBox1.Text;
            //提取店铺地址
            Regex regex = new Regex(@"([^/]*com)");
            Match match = regex.Match(url);
            String shopUrl = match.Value;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + shopUrl + "/category.htm");
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.Headers.Add("Accept-Language", "zh-CN;");
            request.Referer = "www.tmall.com";
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
            request.Headers.Add("Accept-Encoding", "gzip, deflate;");
            request.Host = shopUrl;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            myResponseStream = new GZipStream(myResponseStream, CompressionMode.Decompress);
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            //richTextBox1.Text = retString;

            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(retString);

            HtmlNode rootNode = document.DocumentNode;
            HtmlNodeCollection nodeList = rootNode.SelectNodes("//ul[@class='J_TAllCatsTree cats-tree']/li");

            foreach (HtmlNode node in nodeList)
            {
                HtmlNode fstChild = node.SelectSingleNode(".//a[contains(@class,'fst-cat-name')]");
                richTextBox1.AppendText(fstChild.InnerText.Trim() + "\n");
                HtmlNodeCollection sndChild = node.SelectNodes(".//ul[contains(@class,'fst-cat-bd')]//a[contains(@class,'cat-name')]");
                if (sndChild != null)
                {
                    foreach (HtmlNode sndChildNode in sndChild)
                    {
                        richTextBox1.AppendText("\t" + sndChildNode.InnerText.Trim() + "\n");
                    }
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"([^/]*com)");
            string url = textBox1.Text;

            Match match = regex.Match(url);

            richTextBox1.AppendText(match.Value);

        }
    }
}
